package cn.wuqingna.legendgem.loader

import cn.wuqingna.legendgem.conditions.ConditionGroup
import cn.wuqingna.legendgem.gem.Gem
import cn.wuqingna.legendgem.gem.GemOptions
import cn.wuqingna.legendgem.util.info
import cn.wuqingna.legendgem.util.listFile
import cn.wuqingna.legendgem.util.newfolder
import org.bukkit.configuration.file.YamlConfiguration
import taboolib.common.LifeCycle
import taboolib.common.platform.Awake
import taboolib.module.chat.colored
import taboolib.platform.BukkitPlugin

object GemLoader {

    private val folder = newfolder(BukkitPlugin.getInstance().dataFolder, "gems", listOf("默认宝石.yml"))
    val gems = mutableMapOf<String, Gem>()

    @Awake(LifeCycle.ENABLE)
    fun init(){
        gems.clear()
        folder.listFile("yml").forEach { file ->
            YamlConfiguration.loadConfiguration(file).apply {
                getKeys(false).forEach { key ->
                    val section = this.getConfigurationSection(key)

                    val holePosition = section.getStringList("hole-position").toMutableList().toMutableSet()

                    val attribute = section.getStringList("source-data.attributes")

                    val data = mutableMapOf<String, String> ()

                    val dataSection = section.getConfigurationSection("source-data.cache")

                    dataSection.getKeys(false).forEach { dataKey ->
                        data[dataKey] = dataSection.get(dataKey).toString()
                    }



                    val customData = mutableMapOf<String, String> ()
                    val customDaTaSection = section.getConfigurationSection("custom-data")
                    customDaTaSection.getKeys(false).forEach { dataKey ->
                        customData[dataKey] = customDaTaSection.getString(dataKey)
                    }


                    val options = if (section.isConfigurationSection("options")){
                        val optionsImportGroup : ConditionGroup = if (section.contains("options.import-group")) {
                            ConditionsLoader.conditions[section.getString("options.import-group")]!!
                        } else {
                            ConditionsLoader.getConditionGroup(section.getConfigurationSection("options"))
                        }
                        val embedProbability = section.getString("options.embed-gem-probability", "100")
                        val takeProbability = section.getString("options.take-gem-probability", "100")
                        val broken = section.getBoolean("options.gem-is-broken", false)
                        val protects = section.getStringList("options.protects")
                        val selectGui = section.getStringList("options.select-gui") ?: mutableListOf<String>()
                        GemOptions(
                            optionsImportGroup,
                            embedProbability,
                            takeProbability,
                            broken,
                            protects,
                            selectGui
                        )
                    } else {
                        val gem = section.getString("options")
                        gems[gem]!!.options
                    }


                    val gem = Gem(section, section.getString("icon").colored(), options, holePosition, attribute, data, customData)
                    gems[key] = gem
                }
            }

            info(" Loader Gems §c${gems.size}§f.")
        }
    }


}